package com.spzx.common.rabbit.service;

import com.alibaba.fastjson2.JSON;
import com.spzx.common.core.utils.uuid.UUID;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.rabbit.entity.GuiguCorrelationData;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.concurrent.TimeUnit;

@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 发送消息
     *
     * @param exchange   交换机
     * @param routingKey 路由键
     * @param message    消息
     */
/*    public boolean sendMessage(String exchange, String routingKey, Object message) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        return true;
    }*/
    public boolean sendMessage(String exchange, String routingKey, Object message) {
        String uuid = "mq:"+UUID.randomUUID().toString().replaceAll("-","");
        GuiguCorrelationData guiguCorrelationData = new GuiguCorrelationData();
        guiguCorrelationData.setId(uuid);
        guiguCorrelationData.setMessage(message);
        guiguCorrelationData.setExchange(exchange);
        guiguCorrelationData.setRoutingKey(routingKey);


        //发送消息前，需要将关联数据保存到redis中。为啥？   退回回调函数如果需要重发消息，得从缓存中获取关联数据，利用关系数据信息进行消息重发。
        redisTemplate.opsForValue().set(uuid,guiguCorrelationData,10, TimeUnit.MINUTES); //时间设置比所有发送消息时间和要长一些


        //携带关联数据-附件
        rabbitTemplate.convertAndSend(exchange, routingKey, message,guiguCorrelationData);

        return true;
    }


    /**
     * 专业发送延迟消息
     * @param exchangeDelay
     * @param routingDelay
     * @param msg
     * @param delayTime
     */
    public Boolean sendDealyMessage(String exchangeDelay, String routingDelay, String msg, int delayTime) {

        //1.创建自定义相关消息对象-包含业务数据本身，交换器名称，路由键，队列类型，延迟时间,重试次数
        GuiguCorrelationData correlationData = new GuiguCorrelationData();
        String uuid = "mq:" + UUID.randomUUID().toString().replaceAll("-", "");
        correlationData.setId(uuid);
        correlationData.setMessage(msg);
        correlationData.setExchange(exchangeDelay);
        correlationData.setRoutingKey(routingDelay);
        correlationData.setDelay(true);
        correlationData.setDelayTime(delayTime);


        //2.将相关消息存入Redis  Key：UUID  相关消息对象  10 分钟
        redisTemplate.opsForValue().set(uuid, JSON.toJSONString(correlationData), 10, TimeUnit.MINUTES);

        rabbitTemplate.convertAndSend(exchangeDelay,routingDelay,msg,(message)->{
            message.getMessageProperties().setDelay(delayTime*1000); //时间单位毫秒
            return message;
        },correlationData);

        return true;
    }
}